package com.kyzh.core.pager.weal.zhuanjin

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.GameTaskBean
import com.gushenge.core.requests.GameRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.databinding.FragmentGameplayBinding
import com.kyzh.core.databinding.ItemGametaskBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.dao.MMKVConsts
import com.kyzh.core.dialog.showGamePlayDialog
import org.jetbrains.anko.support.v4.onRefresh

class GamePlayFragment : BaseFragment() {
    private lateinit var  db : FragmentGameplayBinding
    private val adapter = Adapter()
    private var mP = 1
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db =  FragmentGameplayBinding.inflate(inflater)
        return  db.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (MMKVConsts.uid.isEmpty()){
            "请先打开${resources.getString(R.string.app_name)}登录后使用".toast()
            requireActivity().finish()
            return
        }
        initView()
    }

    private fun initView() {
        db.rev.layoutManager = LinearLayoutManager(requireContext())
        db.rev.adapter = adapter
        adapter.setEmptyView(R.layout.empty)
        db.swipe.onRefresh {
            mP = 1
            GameRequest.getGameTask(mP){
                db.swipe.isRefreshing = false
                this?.apply {
                    adapter.setNewInstance(this.data)
                    adapter.loadMoreModule.loadMoreComplete()
                    if(mP>=max_p){
                        adapter.loadMoreModule.loadMoreEnd(true)
                    }
                }
            }
        }
        GameRequest.getGameTask(1){
            this?.apply {
                adapter.setNewInstance(this.data)
                adapter.loadMoreModule.loadMoreComplete()
                if(mP>=max_p){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            mP++
            GameRequest.getGameTask(mP){
                this?.apply {
                    adapter.addData(this.data)
                    adapter.loadMoreModule.loadMoreComplete()
                    if(mP>=max_p){
                        adapter.loadMoreModule.loadMoreEnd(true)
                    }
                }
            }
        }
        adapter.setOnItemClickListener{a,_,p->
            val gameTaskBean = a.data[p] as GameTaskBean
            requireContext().showGamePlayDialog(gameTaskBean)
        }
        adapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id  ){
                R.id.tvStatus ->{
                    val gameTaskBean = adapter.data[position] as GameTaskBean
                    when(gameTaskBean.status){
                        0->{
                            GameRequest.getGameTask(gameTaskBean.id){
                                gameTaskBean.status = 1
                                adapter.notifyItemChanged(position)
                            }
                        }
                        1->{
                            GameRequest.getGameTask(gameTaskBean.id){
                            }
                        }
                        2->{
                            GameRequest.getGameTaskReward(gameTaskBean.id){

                                gameTaskBean.status = 3
                                adapter.notifyItemChanged(position)
                            }
                        }
                        3->{
                            "您已领取过奖励".toast()
                        }
                    }
                }
            }

        }
    }

    private class  Adapter :BaseQuickAdapter<GameTaskBean,BaseDataBindingHolder<ItemGametaskBinding>>(
        R.layout.item_gametask),LoadMoreModule{
        init {
            addChildClickViewIds(R.id.tvStatus)
        }
        override fun convert(
            holder: BaseDataBindingHolder<ItemGametaskBinding>,
            item: GameTaskBean
        ) {
            holder.dataBinding?.apply {
                data = item
                when(item.status){
                    0->{
                        tvStatus.text="报名"
                        tvStatus.setTextColor(Color.parseColor("#FF7D1F"))
                        tvStatus.setBackgroundResource(R.drawable.bg_14dp_gametask_baoming)
                        tvStatus.isEnabled = true
                    }
                    1->{
                        tvStatus.text="进行中"
                        tvStatus.setTextColor(Color.parseColor("#CCCCCC"))
                        tvStatus.setBackgroundResource(R.drawable.bg_14dp_daily_yes)
                        tvStatus.isEnabled = false
                    }
                    2->{
                        tvStatus.text="领奖"
                        tvStatus.setTextColor(Color.parseColor("#29C571"))
                        tvStatus.setBackgroundResource(R.drawable.bg_14dp_gametask_lingjiang)
                        tvStatus.isEnabled = true
                    }
                    3->{
                        tvStatus.text="已领取"
                        tvStatus.setTextColor(Color.parseColor("#CCCCCC"))
                        tvStatus.setBackgroundResource(R.drawable.bg_14dp_daily_yes)
                        tvStatus.isEnabled = false
                    }
                }

            }
        }

    }
}